package com.wuyoucar.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TCarinfo;
import com.wuyoucar.entity.TOrder;
import com.wuyoucar.service.InCarinfoService;
import com.wuyoucar.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private InCarinfoService inCarinfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/getAllOrderByUserid")
    public ResultBean getAllOrderByUserid(Integer userid) {
        ResultBean resultBean = new ResultBean();
        List<TOrder> allOrderByUserid = orderService.getAllOrderByUserid(userid);
        if (allOrderByUserid != null) {
            resultBean.setCode(0);
            resultBean.setData(allOrderByUserid);
            resultBean.setMsg("有订单");
        } else {
            resultBean.setCode(1);
            resultBean.setMsg("无订单");
        }
        return resultBean;
    }

    //购买保险，将数据写入数据库
    @RequestMapping("/addOrder")
    public ResultBean addOrder(@RequestBody LinkedHashMap<String, String> mymap) {
        //生成订单号
        String orderNumber = UUID.randomUUID().toString().split("-")[0];
        String s = stringRedisTemplate.opsForValue().get(mymap.get("userid"));
        String[] cars = s.split(",");
        //获取用户id
        String str1 = cars[0].substring(0, cars[0].indexOf("="));
        String userid = cars[0].substring(str1.length() + 1, cars[0].length());
        //获取车架号
        str1 = cars[2].substring(0, cars[2].indexOf("="));
        String carnumber = cars[2].substring(str1.length() + 1, cars[2].length());
   //     System.out.println("车牌号==" + carnumber);
        //获取车牌号
        str1 = cars[4].substring(0, cars[4].indexOf("="));
        String vin = cars[4].substring(str1.length() + 1, cars[4].length());
   //     System.out.println("车架号==" + vin);
        //获取车型号
        str1 = cars[5].substring(0, cars[5].indexOf("="));
        String cartype = cars[5].substring(str1.length() + 1, cars[5].length());
        //获取下单时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String mytime = df.format(new Date());
        Date ordertime = new Date();
        try {
            ordertime = df.parse(mytime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取报价
        String detailed = mymap.get("detailed");
        double price = Double.parseDouble(detailed);
        TOrder order = new TOrder(orderNumber, Integer.parseInt(userid), vin, carnumber, cartype, 0, ordertime, "1", mymap.get("insuredpersonname"), mymap.get("insuredpersonidnumber"), mymap.get("carmastername"), mymap.get("carmasteridnumber"), 0, "审核通过后可付款", "订单审核中", price,1);
        return orderService.addOrder(order);
    }

}
